package com.huopaolan.yohuoshop.Mvp.View.Activity;

import android.content.ContentValues;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.huopaolan.yohuoshop.Dao.Service.DBService;
import com.huopaolan.yohuoshop.Mvc.Model.MyScrollView;
import com.huopaolan.yohuoshop.R;
import com.huopaolan.yohuoshop.Sp.SpUtils;

import java.util.ArrayList;
import java.util.List;

public class HeightAndWeightActivity extends AppCompatActivity implements OnClickListener {
    private MyScrollView mySV;
    private MyScrollView my;
    private ImageView hei_back;
    private Button user_save;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_height_and_weight);
        mySV = findViewById(R.id.mySV);
        my = findViewById(R.id.my);
        hei_back = findViewById(R.id.iv_sel_user_hw_back);
        user_save = findViewById(R.id.n_sel_user_hw_share);
        user_save.setOnClickListener(this);
        hei_back.setOnClickListener(this);
        final List<String> dataList = new ArrayList<>();
        for (int i = 50; i <= 250; i++) {
            dataList.add("" + i);
        }
        mySV.setDataList(dataList);
        mySV.setSelectItemListener(new MyScrollView.SelectItemListener() {
            @Override
            public void selectItem(int index) {
                if (index < 0 || index > 200) {
                    return;
                }
                String s_height = dataList.get(index);
                String spReadUser = SpUtils.getInstance().SpReadUser();
                ContentValues values = new ContentValues();
                values.put("user_height", s_height + "CM");
                int user = DBService.getDbService().update("User", values, "user_id=?", new String[]{spReadUser});
                if (user > 0) {
                    Log.d("HEIGHT", "修改成功");
                    DBService.getDbService().close();
                }
            }
        });
        final List<String> data = new ArrayList<>();
        for (int i = 10; i <= 140; i++) {
            data.add("" + i);
        }
        my.setDataList(data);
        my.setSelectItemListener(new MyScrollView.SelectItemListener() {
            @Override
            public void selectItem(int index) {
                Toast.makeText(HeightAndWeightActivity.this, "" + index, Toast.LENGTH_SHORT).show();
                if (index < 0 || index > 130) {
                    return;
                }
                String s_weight = data.get(index);
                String spReadUser = SpUtils.getInstance().SpReadUser();
                ContentValues values = new ContentValues();
                values.put("user_weight", s_weight + "KG");
                int user = DBService.getDbService().update("User", values, "user_id=?", new String[]{spReadUser});
                if (user > 0) {
                    Log.d("HEIGHT", "修改成功");
                    DBService.getDbService().close();
                }
            }
        });
    }

    @Override
    protected void onStart() {
        Window window = getWindow();
        window.setStatusBarColor(Color.BLACK);
        super.onStart();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_sel_user_hw_back:
                finish();
                break;
            case R.id.n_sel_user_hw_share:
                finish();
                break;
        }
    }
}
